package com.chocho.producerserver.controller;

import com.chocho.producerserver.service.StatisticService;
import com.chocho.producerserver.utils.Msg;
import com.chocho.producerserver.vo.VHost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by chocho on 2022/4/12.
 */
@CrossOrigin
@RestController
@RequestMapping("/statistic")
@PreAuthorize("hasAuthority('statistic')")
public class StatisticController {
    private Logger logger = LoggerFactory.getLogger(StatisticController.class);

    @Autowired
    private StatisticService statisticService;

    //获取访问网站人数
    @GetMapping("/get/enter")
    @ResponseBody
    public Msg getEnterWebsiteNum(){
        return Msg.success().add("visitedNum", statisticService.getVisitedNum());
    }

    //TODO 后面使用拦截器实现，统计网站人数
    //一旦页面被访问，则将访问信息存入数据库
    @GetMapping("/set/enter")
    @ResponseBody
    public Msg setEnterWebsiteNum(HttpServletRequest request){
        return statisticService.setVisitedNum(request) ? Msg.success() : Msg.fail();
    }

    //首页统计量
    @GetMapping("/host")
    @ResponseBody
    public Msg getHostData(){
        VHost host = statisticService.getHostData();
        return (host != null) ? Msg.success().add("host", host) : Msg.fail();
    }
}
